package com.recordhistory.service;

import com.recordhistory.Constant;
import com.recordhistory.dao.EventOccurDao;
import com.recordhistory.dao.HistoryImgDao;
import com.recordhistory.exception.ServiceException;
import com.recordhistory.model.EventOccur;
import com.recordhistory.model.HistoryImg;
import com.recordhistory.utils.CheckTool;
import com.recordhistory.utils.CommonTool;
import com.recordhistory.utils.FileTool;
import com.recordhistory.vo.EventOccurVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：LX
 * 创建时间： 2019/6/13. 9:57
 * 地点：广州
 * 目的: 事件
 * 备注说明：
 */
@Service
public class EventOccurService {

    @Resource
    private EventOccurDao eventOccurDao;
    @Resource
    private HistoryImgDao historyImgDao;


    /**
     * 图片的基址
     * 默认读取的是application.properties
     */
    @Value("${base.img}")
    private String basePathImg;


    /**
     * 检查 事件必填参数
     * @param eventOccur 事件data
     */
    private void checkEventOccur(EventOccur eventOccur){
        if (eventOccur == null){
            throw new ServiceException("不能添加空数据");
        }
        if (CheckTool.checkNull(eventOccur.getHistoryId())){
            throw new ServiceException("请选择历史主线");
        }
        if (CheckTool.checkNull(eventOccur.getContent())){
            throw new ServiceException("事件不能为空");
        }

        /*月份填写了，就不能填写季节
        填写了发生的日子，就不能缺月份
        填写了季节，就不能填写月份和发生的日子*/
        if (eventOccur.getMonth() != null){
            if (!CheckTool.checkNull(eventOccur.getSeasons())){
                throw new ServiceException("月份和季节不能同时选择，只能选择其中一个");
            }
            if (eventOccur.getMonth() < 1 || eventOccur.getMonth() > 12){
                throw new ServiceException("月份输入错误，不能小于一月或大于十二月");
            }
        }
        if (eventOccur.getDateOccur() != null){
            if (eventOccur.getMonth() == null){
                throw new ServiceException("当选择指定天时，还必须同时选择月份");
            }
            if (eventOccur.getDateOccur() < 1 || eventOccur.getDateOccur() > 31){
                throw new ServiceException("日期输入错误，不能小于一号或大于三十一号");
            }
        }
        if (!CheckTool.checkNull(eventOccur.getSeasons())){
            if (eventOccur.getMonth() != null){
                throw new ServiceException("月份和季节不能同时选择，只能选择其中一个");
            }
        }
        EventOccur eventOccurSQL = eventOccurDao.selectEventByEven(eventOccur.getContent(), eventOccur.getId());
        if (eventOccurSQL != null){
            throw new ServiceException("事件已经添加，请勿重复添加");
        }

        if (CheckTool.checkNull(eventOccur.getImportant())){
            eventOccur.setImportant(Constant.ordinary);
        }
    }


    /**
     * 添加 事件
     * @param eventOccur 事件data
     */
    @Transactional(rollbackFor = Exception.class)
    public void addEventOccur(EventOccur eventOccur){
        checkEventOccur(eventOccur);

        eventOccur.setId(CommonTool.getUUId());
        eventOccur.setCreateDate(CommonTool.getTimestamp());
        eventOccur.setUpdateDate(CommonTool.getTimestamp());

        eventOccurDao.insert(eventOccur);
    }


    /**
     * 更新 事件
     * @param eventOccur 事件data
     */
    @Transactional(rollbackFor = Exception.class)
    public void updEventOccur(EventOccur eventOccur){
        checkEventOccur(eventOccur);

        if (CheckTool.checkNull(eventOccur.getId())){
            throw new ServiceException("ID为空，无法更新");
        }

        eventOccur.setUpdateDate(CommonTool.getTimestamp());
        eventOccurDao.updateById(eventOccur);
    }


    /**
     * 删除 事件
     * @param id 事件ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void delEventOccur(String id){
        if (CheckTool.checkNull(id)){
            throw new ServiceException("ID不能为空");
        }

        // 删除各种关联信息
        List<HistoryImg> historyImgList = historyImgDao.selectImgByEventOccurId(id);
        if (historyImgList != null && historyImgList.size() > 0){
            for (HistoryImg historyImg : historyImgList){
                if (!CheckTool.checkNull(historyImg.getPath())){
                    FileTool.delectFile(basePathImg + historyImg.getPath());
                }
            }
        }

        eventOccurDao.deleteById(id);
    }

    /**
     * 根据 历史主线查找事件 ，查询季节相关并排序
     * @param historyId 历史主线ID
     * @return
     */
    public List<EventOccurVo> selectEventBySeason(String historyId) {
        return eventOccurDao.selectEventBySeason(historyId);
    }

    /**
     * 根据 历史主线查找事件 ，查询年度相关并排序
     * @param historyId 历史主线ID
     * @return
     */
    public List<EventOccurVo> selectEventByYear(String historyId) {
        return eventOccurDao.selectEventByYear(historyId);
    }

    /**
     * 根据 历史主线查找事件 ，查询事件相关并排序
     * @param historyId 历史主线ID
     * @return
     */
    public List<EventOccurVo> selectEventByDate(String historyId) {
        return eventOccurDao.selectEventByDate(historyId);
    }


    /**
     * 根据主键查询 事件
     * @param id 事件主键
     * @return
     */
    public EventOccur selectById(String id){
        return eventOccurDao.selectById(id);
    }
}
